package com.allenlee.mvphelper.net.retrofit.exception;


import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import io.reactivex.functions.Consumer;

/**
 * @author : lifei
 * 创建时间： 19-11-29
 * 更改时间： 19-11-29
 * Email : allen.lee723@qq.com
 * @version :
 * Description：
 */
public abstract class MHConsumer implements Consumer<Throwable> {

    public abstract void onError(String msg);

    @Override
    public void accept(Throwable throwable) throws Exception {
        if (throwable != null) {
            if (throwable instanceof APIException) {
                APIException exception = (APIException) throwable;
                onError(exception.getMessage());
            } else if (throwable instanceof ConnectException || throwable instanceof UnknownHostException) {
                onError("网络异常");
            } else if (throwable instanceof SocketTimeoutException) {
                onError("网络连接超时");
            } else {
                onError(throwable.getMessage());
            }
        }
    }
}
